#!/bin/sh
export SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

# Down the last running theme
if [ -f "/tmp/leftwm-theme-down" ]; then
    /tmp/leftwm-theme-down
    rm /tmp/leftwm-theme-down
fi
ln -s $SCRIPTPATH/down /tmp/leftwm-theme-down

# Boot picom
if [ -x "$(command -v picom)" ]; then
    picom --daemon --experimental-backends &> /dev/null
fi

# Set the theme.toml config
echo "LoadTheme $SCRIPTPATH/theme.toml" > $XDG_RUNTIME_DIR/leftwm/commands.pipe

# Set background
if [ -x "$(command -v feh)" ]; then
    if [ -f "$SCRIPTPATH/background.jpg" ]; then
        feh --bg-fill --no-fehbg -z $(head -n 1 "$SCRIPTPATH/bg-dir")
    else
        feh --bg-fill --no-fehbg $SCRIPTPATH/background.jpg
    fi
fi

# Conky (not yet configured)
#~/.config/conky/start-conky.sh

# Polybar
monitors="$(polybar -m | wc -l)"
for i in `seq 1 $monitors`;
do
    echo $i
    monitor="$(polybar -m | sed -n ${i}p | sed s/:.*// | tac)"
    echo $monitor 
    monitor=$monitor polybar -c $SCRIPTPATH/polybar.ini mainbar &> /dev/null &
done
